var page_size = 10;
var arr_data;
var current_user = "";
var is_admin = 0;


function getXMLHttpRequest() {
    var xmlhttp = null;
    if (window.XMLHttpRequest)//自动检测当前浏览器的版本，如果是 IE5.0 以上的高版本的浏览器
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();//创建请求对象
    }
    else////如果浏览器是底版本的
    {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//创建请求对象
    }
    return xmlhttp;//返回请求对象
}

//清空表格保留表头
function clear_table() {
    var table = document.getElementById("table");
    while (table.rows.length > 1)
        table.deleteRow(1);
}

//设置表格数据
function set_table(arr_data) {
    var table = document.getElementById("table");
    clear_table();
    for (var i = 0; i < arr_data.length / 7; i++) {
        var newRow = table.insertRow();  //插入新行
        for (var j = 0; j < 7; j++) {
            var cell = newRow.insertCell();
            if (arr_data[i * 7 + j] == "(null)")
                cell.textContent = "";
            else if (arr_data[i * 7 + j] == "0")
                cell.textContent = "正常";
            else if (arr_data[i * 7 + j] == "1")
                cell.textContent = "迟到";
            else if (arr_data[i * 7 + j] == "2") {
                cell.textContent = "早退";
                cell.textColor = "red";
            }
            else
                cell.textContent = arr_data[i * 7 + j];
        }
    }
}

function load_fun() {
    var params = new URLSearchParams(window.location.search);
    current_user = params.get('user');
    is_admin = params.get('purview');
    // if (current_user.length == 0) {
    //     window.location.href = "login.html";
    // }
    document.getElementById("check").innerText = '考勤情况';

    if (is_admin == 0) {
        document.getElementById("check").innerText = (current_user + '考勤情况');
        document.getElementById("user_div").hidden = true;
        document.getElementById("card_div").hidden = true;
        document.getElementById("update").hidden = true;
        document.getElementById("delete").hidden = true;
        document.getElementById('get_card_btn').hidden = true;
    }

    set_select_data();
    var page_num = document.getElementById('page_num');
    //1、创建对象
    var xmlHttp = getXMLHttpRequest();
    //2、设置回调函数
    xmlHttp.onreadystatechange = function () {
        console.log('xmlHttp.readyState==' + xmlHttp.readyState + ' ' + 'xmlHttp.status==' + xmlHttp.status);
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {

            //获取服务器的应答（假如服务器的应答是字符串格式）
            var ret = xmlHttp.responseText;
            console.log(ret);
            //分割字符串
            arr_data = ret.split(" ");
            //删除字符串数组尾元素
            arr_data.pop();

            set_table(arr_data);
        }
    }

    if (is_admin == 1) {
        var url = "/cgi-bin/search.cgi?admin";
        url += ':' + page_size;
        url += ':' + (page_num.textContent - 1);

        if (user.value.length != 0)
            url += ':' + user.value;
        else
            url += ':' + '*';

        if (card.value.length != 0)
            url += ':' + card.value;
        else
            url += ':' + '*';

        // url += ':' + year_data + '-' + month_data + '-' + day_data;
        url += ':*-*-*';
    }
    else {
        var url = "/cgi-bin/search.cgi?user";
        url += ':' + page_size;
        url += ':' + (page_num.textContent - 1);
        url += ':' + current_user;
        url += ':*-*-*';
    }

    xmlHttp.open("GET", url, true);
    //4、发送请求
    xmlHttp.send();
}


function set_select_data() {

    //获取当前时间
    var date = new Date();
    date.getFullYear();
    date.getMonth();
    date.getDate();
    var year_now = date.getFullYear();
    var month_now = (date.getMonth() + 1);
    month_now = month_now < 10 ? ('0' + month_now) : month_now;
    var day_now = date.getDate();
    // console.log("year:" + year_now);
    // console.log("month:" + month_now);
    // console.log("day:" + day_now);

    //设置复选框时间
    var year = document.getElementById('year_select');
    var month = document.getElementById('month_select');
    var day = document.getElementById('day_select');
    var month_31 = ['1', '3', '5', '7', '8', '10', '12'];
    var month_30 = ['4', '6', '5', '9', '11'];

    //清除月份样式
    // month.innerHTML = "";
    // for (var i = 1; i <= 12; i++) {
    //     if (i < 10)
    //         month.options.add(new Option(i, '0' + i));
    //     else
    //         month.options.add(new Option(i, i));
    // }
    //清除日期样式

    day.innerHTML = "";
    if (month_31.includes(month.options[month.selectedIndex].text)) {
        for (var i = 1; i <= 31; i++) {
            if (i < 10)
                day.options.add(new Option(i, '0' + i));
            else
                day.options.add(new Option(i, i));
        }
    }
    else if (month_30.includes(month.options[month.selectedIndex].text)) {
        for (var i = 1; i <= 30; i++) {
            if (i < 10)
                day.options.add(new Option(i, '0' + i));
            else
                day.options.add(new Option(i, i));
        }
    }
    else if (month.options[month.selectedIndex].text == 2) {
        if (((year.value % 4 == 0) && (year.value % 100 != 0)) || (year.value % 400 == 0)) {
            for (var i = 1; i <= 29; i++) {
                if (i < 10)
                    day.options.add(new Option(i, '0' + i));
                else
                    day.options.add(new Option(i, i));
            }
        }
        else {
            for (var i = 1; i <= 28; i++) {
                if (i < 10)
                    day.options.add(new Option(i, '0' + i));
                else
                    day.options.add(new Option(i, i));
            }
        }
    }
}

function up_data() {
    var user = document.getElementById('user').value;
    var card = document.getElementById('card').value;
    if ((user.length > 0 && user.length < 6) || (card.length > 0 && card.length != 8)) {
        alert("输入的用户名长度少于6位，或查询的卡号长度不为8位，请重新输入!!!");
        return;
    }
    // var year = document.getElementById('year_select');
    // var month = document.getElementById('month_select');
    // var day = document.getElementById('day_select');
    // var year_data = year.options[year.selectedIndex].value;
    // var month_data = month.options[month.selectedIndex].value;
    // var day_data = day.options[day.selectedIndex].value;
    var is_like = document.getElementById('is_like_checkbox').checked;

    //存储cgi接收到的数据
    //1、创建对象
    var xmlHttp = getXMLHttpRequest();
    //2、设置回调函数
    xmlHttp.onreadystatechange = function () {
        console.log('xmlHttp.readyState==' + xmlHttp.readyState + ' ' + 'xmlHttp.status==' + xmlHttp.status);
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            //获取服务器的应答（假如服务器的应答是字符串格式）
            var ret = xmlHttp.responseText;
            console.log(ret);
            //分割字符串
            arr_data = ret.split(" ");
            //删除字符串数组尾元素
            arr_data.pop();
            //设置表格
            set_table(arr_data);
        }
    }
    //组包
    var url = "/cgi-bin/search.cgi?admin";
    url += ':' + page_size;
    url += ':' + (page_num.textContent - 1);

    if (user.length != 0)
        url += ':' + user;
    else
        url += ':' + '*';

    if (card.length != 0)
        url += ':' + card;
    else
        url += ':' + '*';

    // url += ':' + year_data + '-' + month_data + '-' + day_data;
    url += ':*-*-*';
    xmlHttp.open("GET", url, true);
    //4、发送请求
    xmlHttp.send();
}

//上一页
function previousPage() {
    var page_num = document.getElementById('page_num');
    //判断当前数据页面
    if (page_num.textContent > 1)
        page_num.textContent--;
    else
        return;


    //1、创建对象
    var xmlHttp = getXMLHttpRequest();
    //2、设置回调函数
    xmlHttp.onreadystatechange = function () {
        console.log('xmlHttp.readyState==' + xmlHttp.readyState + ' ' + 'xmlHttp.status==' + xmlHttp.status);
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            clear_table();
            //获取服务器的应答（假如服务器的应答是字符串格式）
            var ret = xmlHttp.responseText;
            console.log(ret);
            // console.log(ret);
            //分割字符串
            arr_data = ret.split(" ");
            //删除字符串数组尾元素
            arr_data.pop();

            set_table(arr_data);
        }
    }
    //3、创建请求 GET方式 异步
    //get表示从0号设备获取卡号
    if (is_admin == 1) {
        var url = "/cgi-bin/search.cgi?admin";
        url += ':' + page_size;
        url += ':' + (page_num.textContent - 1);

        if (user.value.length != 0)
            url += ':' + user.value;
        else
            url += ':' + '*';

        if (card.value.length != 0)
            url += ':' + card.value;
        else
            url += ':' + '*';

        // url += ':' + year_data + '-' + month_data + '-' + day_data;
        url += ':*-*-*';
    }
    else {
        var url = "/cgi-bin/search.cgi?user";
        url += ':' + page_size;
        url += ':' + (page_num.textContent - 1);
        url += ':' + current_user;
        url += ':*-*-*';
    }

    xmlHttp.open("GET", url, true);
    //4、发送请求
    xmlHttp.send();
}

//下一页
function nextPage() {
    var page_num = document.getElementById('page_num');
    var table = document.getElementById('table');

    var user = document.getElementById('user');
    var card = document.getElementById('card');
    var year = document.getElementById('year_select');
    var month = document.getElementById('month_select');
    var day = document.getElementById('day_select');
    // var year_data = selectElement.value;
    // var month_data = selectElement.value;
    // var day_data = selectElement.value;

    if (table.rows.length < 11)
        return;
    page_num.textContent++;

    //1、创建对象
    var xmlHttp = getXMLHttpRequest();
    //2、设置回调函数
    xmlHttp.onreadystatechange = function () {
        console.log('xmlHttp.readyState==' + xmlHttp.readyState + ' ' + 'xmlHttp.status==' + xmlHttp.status);
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            clear_table();
            //获取服务器的应答（假如服务器的应答是字符串格式）
            var ret = xmlHttp.responseText;

            //分割字符串
            arr_data = ret.split(" ");
            //删除字符串数组尾元素
            arr_data.pop();

            set_table(arr_data);
        }
    }
    //admin:(size):(page):(user):(card):(year)-(month)-(day)
    //user:(size):(page):(now_user):(year):(month):(day):(is_like)
    if (is_admin == 1) {
        var url = "/cgi-bin/search.cgi?admin";
        url += ':' + page_size;
        url += ':' + (page_num.textContent - 1);

        if (user.value.length != 0)
            url += ':' + user.value;
        else
            url += ':' + '*';

        if (card.value.length != 0)
            url += ':' + card.value;
        else
            url += ':' + '*';

        // url += ':' + year_data + '-' + month_data + '-' + day_data;
        url += ':*-*-*';
    }
    else {
        var url = "/cgi-bin/search.cgi?user";
        url += ':' + page_size;
        url += ':' + (page_num.textContent - 1);
        url += ':' + current_user;
        url += ':*-*-*';
    }

    xmlHttp.open("GET", url, true);
    //4、发送请求
    xmlHttp.send();
}

function update_btn() {
    var page_num = document.getElementById('page_num');
    var table = document.getElementById('table');
    var user = document.getElementById('user').value;
    var card = document.getElementById('card').value;
    if (user.length > 0 && user.length < 6) {
        alert("输入的用户名长度少于6位，请重新输入!!!");
        return;
    }

    // if (table.rows.length < 11)
    //     return;
    page_num.textContent = 1;

    //1、创建对象
    var xmlHttp = getXMLHttpRequest();
    //2、设置回调函数
    xmlHttp.onreadystatechange = function () {
        console.log('xmlHttp.readyState==' + xmlHttp.readyState + ' ' + 'xmlHttp.status==' + xmlHttp.status);
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            clear_table();
            //获取服务器的应答（假如服务器的应答是字符串格式）
            var ret = xmlHttp.responseText;

            load_fun();

        }
    }


    var url = "/cgi-bin/update.cgi?admin";
    url += ':' + user;
    url += ':' + card;

    xmlHttp.open("GET", url, true);
    //4、发送请求
    xmlHttp.send();
    console.log(url);
}

function delete_btn() {
    var page_num = document.getElementById('page_num');
    var table = document.getElementById('table');

    var user = document.getElementById('user').value;
    if (user.length > 0 && user.length < 6) {
        alert("输入的用户名长度少于6位，请重新输入!!!");
        return;
    }
    var card = document.getElementById('card').value;
    if (card.length == 0) {
        alert('请刷卡获取卡号');
        return;
    }
    else if (card.length != 8) {
        alert('卡号错误，请输入8位卡号');
        return;
    }
    // var card = document.getElementById('card');

    //1、创建对象
    var xmlHttp = getXMLHttpRequest();
    //2、设置回调函数
    xmlHttp.onreadystatechange = function () {
        console.log('xmlHttp.readyState==' + xmlHttp.readyState + ' ' + 'xmlHttp.status==' + xmlHttp.status);
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            clear_table();
            //获取服务器的应答（假如服务器的应答是字符串格式）
            var ret = xmlHttp.responseText;

            load_fun();
        }
    }

    var url = "/cgi-bin/delete.cgi?admin";
    url += ':' + user;
    url += ':' + card;


    xmlHttp.open("GET", url, true);
    //4、发送请求
    xmlHttp.send();

    console.log(url);
}

function get_card() {
    //1、创建对象
    var xmlHttp = getXMLHttpRequest();
    //2、设置回调函数
    xmlHttp.onreadystatechange = function () {
        console.log('xmlHttp.readyState==' + xmlHttp.readyState + ' ' + 'xmlHttp.status==' + xmlHttp.status);
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            //获取服务器的应答（假如服务器的应答是字符串格式）
            var ret = xmlHttp.responseText;
            if (ret == "1")
                alert("卡号存在 请换卡");
            //解析ret 使用docunment.getElementById局部更新网页
            document.getElementById("card").value = ret;
        }
    }
    //3、创建请求 GET方式 异步
    //get表示从0号设备获取卡号
    var url = "/cgi-bin/get_card.cgi?get:0";
    xmlHttp.open("GET", url, true);
    //4、发送请求
    xmlHttp.send();
}



